<template>
  <div class="app-container">
    <div class="filter-container">
      <div><el-button type="primary" plain size="mini" @click="openDialog">添加活动类型</el-button></div>
      <!-- 活动类型管理 -->
      <el-table :data="tableData" style="width: 100%" v-loading="loading">
        <el-table-column prop="activityType" label="活动类型"></el-table-column>
        <!-- <el-table-column prop="rewardType" label="奖励类型"></el-table-column> -->
        <el-table-column prop="createTime" label="添加时间">
          <template v-slot="scope">
            {{ parseTime(scope.row.createTime) }}
          </template>
        </el-table-column>
        <el-table-column prop="remark" label="备注说明"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button @click="handleClick(scope.row)" type="text" size="small">编辑</el-button>
            <el-button @click="del(scope.row)" type="text" size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNumber" :limit.sync="queryParams.pageSize" @pagination="getList" />
    </div>
    <!-- 添加活动类型 -->
    <el-dialog :title="title" :visible.sync="dialogVisible" width="30%">
      <el-form ref="form" :model="form" label-width="80px" :rules="rules">
        <el-form-item label="活动类型" prop="activityType"><el-input v-model="form.activityType"></el-input></el-form-item>
        <!-- <el-form-item label="奖励类型" prop="rewardType"><el-input v-model="form.rewardType"></el-input></el-form-item> -->
        <el-form-item label="备注" prop="remark"><el-input v-model="form.remark"></el-input></el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirm" :loading="requesting">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { list, add, update, deleteactiveity } from '@/api/activity/activeitytype';
export default {
  data() {
    return {
      dialogVisible: false,
      form: {},
      queryParams: {
        pageNumber: 1,
        pageSize: 20
      },
      title: '添加活动类型',
      tableData: [],
      loading: true,
      total: 0,
      rules: {
        activityType: [{ required: true, message: '请填写活动类型', trigger: 'blur' }],
        // rewardType: [{ required: true, message: '请填写奖励类型', trigger: 'blur' }],
        remark: [{ required: true, message: '请填写备注', trigger: 'blur' }]
      },
      isEdit: false,
      requesting: false
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      list(this.queryParams).then(res => {
        console.info(res);
        this.tableData = res.result.list;
        this.loading = false;
        this.total = res.result.total;
      });
    },
    openDialog() {
      this.dialogVisible = true;
      this.title = '添加活动类型';
      this.isEdit = false;
      this.form = {};
    },
    handleClick(row) {
      this.dialogVisible = true;
      this.title = '编辑活动类型';
      this.form = Object.assign({}, row);
      this.isEdit = true;
    },
    confirm() {
      // 校验
      this.$refs['form'].validate(valid => {
        if (valid) {
          // 校验成功
          if (this.requesting) return;
          this.requesting = true;

          if (this.isEdit) {
            update(this.form).then(res => {
              this.$message({ type: 'success', message: res.message || '操作成功' });
              this.getList();
              this.requesting = false;
              this.dialogVisible = false
            });
          } else {
            add(this.form).then(res => {
              this.$message({ type: 'success', message: res.message || '操作成功' });
              this.getList();
              this.requesting = false;
              this.dialogVisible = false
            });
          }
        } else {
          return;
        }
      });
    },
    // 删除活动
    del(item) {
      deleteactiveity({
        id: item.id
      }).then(res => {
        this.getList();
        this.$message({ type: 'success', message: res.message || '删除成功' });
      })
    }
  }
};
</script>

<style></style>
